package com.qiangesoft.cloud.sentinel.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/test")
public class TestController {

    @GetMapping("/add")
    public String add() {
        return "add";
    }

    @GetMapping("/a")
    public String testA() {
        return "testA";
    }

    @GetMapping("/b")
    public String testB() {
        return "testB";
    }

    @GetMapping("/c")
    public String testC() {
        return "testC";
    }

    @GetMapping("/d")
    public String testD() {
        int age = 10 / 0;
        return "异常比例";
    }

    @GetMapping("/e")
    public String testE() {
        int age = 10 / 0;
        return "异常数";
    }

    @GetMapping("/hot")
//    @SentinelResource(value = "hot") // 无配置
//    @SentinelResource(value = "hot", blockHandler = "blockHandler") // sentinel配置
//    @SentinelResource(value = "hot", fallback = "fallback") // java异常配置
    @SentinelResource(value = "hot", blockHandler = "blockHandler", fallback = "fallback", exceptionsToIgnore = {IndexOutOfBoundsException.class})
    // 两者，触发sentinel配置规则时
    public String testHot(@RequestParam(value = "p1", required = false) String p1,
                          @RequestParam(value = "p2", required = false) String p2) {
        if ("00".equals(p1)) {
            throw new IndexOutOfBoundsException("fddddddddddd");
        } else {
            int age = 10 / 0;
        }
        return "热点规则";
    }

    public String blockHandler(String p1, String p2, BlockException exception) {
        return "热点规则:1111111111111111111";
    }

    public String fallback(String p1, String p2, Throwable e) {
        return "热点规则:2222222222222222222";
    }
}
